package com.ishansong.ticket.utils;


import com.ishansong.service.common.log.BaseLog;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;

import java.util.concurrent.ConcurrentHashMap;

/**
 * <pre>
 * 功能描述：
 * 订单状态管理
 * 1. 状态值的大的比状态值小的发生时间晚（重新派单除外，已抢单在完成之前都可以人工介入重新派单）
 * 2. 相邻的状态间至少留一个空值，以备后续扩展用
 *
 * </pre>
 * Author: nimin
 * DateTime: 2015-03-16 11:28
 */
public class TaskStatus {
    private static Logger logger = BaseLog.getInstance(TaskStatus.class);

    //订单已关闭
    public static final int CLOSED = 0;

    //--------下单阶段

    //待支付
    public static final int PENDING_PAY = 15;
    //待抢单
    public static final int PENDING_GRAB = 20;
    //重新派单
    public static final int REASSIGN = 21;

    //--------推送、抢单阶段
    //已抢单
    public static final int GRABED = 30;


    //--------取件阶段
    //给寄件人打电话
    public static final int CALLED_SENDER = 40;
    //已就位
    public static final int IN_PLACE = 42;
    //已取件
    public static final int PICKEDUP = 44;

    //--------送件阶段
    //派送中
   // public static final int ON_DELIVERY = 50;
    //给收件人打电话
    public static final int CALLED_RECEIVER = 52;

    //--------结束阶段
    //完成
    public static final int FINISHED = 60;
    //流单
    public static final int ABORTED = 64;


    //任务流转路线
    public static ConcurrentHashMap<String, String> routeTable = new ConcurrentHashMap<String, String>();
    static {
        //待支付  ---->  待抢单、关闭
        routeTable.put("" + PENDING_PAY, PENDING_GRAB + "," + CLOSED + ",");
        //待抢单  ---->  已抢单、流单
        routeTable.put("" + PENDING_GRAB, GRABED + "," + ABORTED + ",");

        //已抢单  ---->  致电寄件人、已就位、已取件、已抢单、重新派单、流单
        //（后三个状态为人工介入）
        routeTable.put("" + GRABED, CALLED_SENDER + "," + IN_PLACE + "," + PICKEDUP + "," + GRABED + "," + REASSIGN + "," + ABORTED + ",");
        //重新派单  ---->  已抢单、流单
        routeTable.put("" + REASSIGN, GRABED + "," + REASSIGN + ","+ ABORTED + ",");
        //致电寄件人  ---->  已就位、已取件、已抢单、重新派单、流单
        //（后三个状态为人工介入）
        routeTable.put("" + CALLED_SENDER, IN_PLACE + "," + PICKEDUP + "," + GRABED + "," + REASSIGN + "," + ABORTED + ",");
        //已就位  ---->  已取件、已抢单、重新派单、流单
        //（后三个状态为人工介入）
        routeTable.put("" + IN_PLACE, PICKEDUP + "," + REASSIGN + "," + GRABED + "," + ABORTED + ",");

        //已取件  ---->  派送中、致电收件人、完成、已抢单、流单
        //（后两个状态为人工介入）
        routeTable.put("" + PICKEDUP,  CALLED_RECEIVER + "," + REASSIGN + "," + FINISHED + "," + GRABED + "," + ABORTED + ",");
        //派送中  ---->  致电收件人、完成、已抢单、流单
        //（后两个状态为人工介入）
       // routeTable.put("" + ON_DELIVERY, CALLED_RECEIVER + "," + FINISHED + "," + GRABED + "," + ABORTED + ",");
        //致电收件人  ---->  完成、已抢单、流单
        //（后两个状态为人工介入）
        routeTable.put("" + CALLED_RECEIVER, FINISHED+ "," + REASSIGN + "," + GRABED + "," + ABORTED + ",");

        routeTable.put("" + FINISHED, "");
        routeTable.put("" + ABORTED, "");
        routeTable.put("" + CLOSED, "");
    }

    /**
     * 根据status获取名称
     *
     * @param status
     * @return
     */
    public static String getNameOfStatus(int status) {
        if (status == ABORTED) {
            return "已取消";
        }
        if (status == FINISHED) {
            return "已完成";
        }
        if (status >= PICKEDUP) {
            return "派送中";
        }
        if (status >= IN_PLACE) {
            return "已就位";
        }

        if (status >= GRABED) {
            return "已抢单";
        }
        if (status >= PENDING_GRAB) {
            return "待抢单";
        }
        return "待支付";
    }

    /**
     * 检查任务状态变更是否可以正常进行
     *
     * @param taskNumber
     * @param currentStatus
     * @param nextStatus
     * @return
     */
    public static boolean checkStatus(String taskNumber, int currentStatus, int nextStatus) {
        if (currentStatus == CLOSED || currentStatus == FINISHED || currentStatus == ABORTED) {
            logger.error("任务已结束，不能进行状态变更：" + taskNumber + " " + currentStatus + " --> " + nextStatus);
            return false;
        }

        String validStatus = routeTable.get("" + currentStatus);
        String[] validStatusArr = validStatus.split(",");
        for(String status : validStatusArr){
            if(!StringUtils.isEmpty(status)){
                int istatus = Integer.parseInt(status);
                if(istatus==nextStatus){
                    logger.info("正常任务状态变更：" + taskNumber + " " + currentStatus + " --> " + nextStatus);
                    return true;
                }
            }

        }

        logger.error("禁止任务状态变更：" + taskNumber + " " + currentStatus + " --> " + nextStatus);
        return false;
    }
}
